<template>
  <div class="page-container">
    <div class="top-bar">
      <!-- <div class="logo-pic">logo图片</div> -->
    </div>
    <div class="login-container">
      <el-form ref="loginForm"
               :model="loginForm"
               :rules="loginRules"
               class="login-form"
               auto-complete="on"
               label-position="left">

        <div class="title-container">
          <h3 class="title">账号登录</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入密码"
                    name="password"
                    show-password
                    @keyup.enter.native="handleLogin" />

        </el-form-item>
        
        <!-- <el-form-item label="请选择设备"
                      prop="equipment"
                      class="equipments">
          <el-radio-group v-model="loginForm.equipment">
            <el-radio v-for="item in equipments"
                      :key="item.id"
                      :label="item">
              {{ item.equipmentName }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->

        <el-button :loading="loading"
                   type="primary"
                   style="width:100%;margin-bottom:30px;"
                   @click.native.prevent="handleLogin">登录
        </el-button>

        <div class="registered">
          没有账号？<span class="title" @click="registered">立即注册</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getEquipment } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      equipments: [],     //设备
      loginForm: {
        username: '123456',
        password: '123456',
        equipment: null
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        equipment: [{ required: true, message: ' 请选择设备' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },

  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // // 获取设备
    // getEquipment()
    //   .then((res) => {
    //     console.log('获取设备成功：', res)
    //     // 只留下名字和id
    //     if(res.code == 200){
    //       this.equipments = res.equipment.map((item) => {
    //         return { equipmentName: item.equipmentName, id: item.id }
    //       })
    //     }
        
    //   })
    //   .catch((err) => {
    //     console.log('获取设备失败：', err)
    //   })
  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'

      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        console.log(this.loginForm);
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 注册
    registered(){
      this.$router.push({ path: '/registered' })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$page_bg: #f7f9fe;
$dark_gray: #889aa4;
$light_gray: #dfe4ed;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .el-form-item__content {
    display: flex;
    height: 40px;
    line-height: 40px;
    border: 1px solid $light_gray;
    border-radius: 5px;

    .svg-container {
      width: 34px;
      padding: 0px 10px;
      color: $dark_gray;
      box-sizing: border-box;
    }

    /* reset element-ui css */
    .el-input {
      flex: 1;
      input {
        height: 40px;
        color: $dark_gray;
        border-radius: 0px 5px 5px 0px;
        -webkit-appearance: none;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $page_bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }
  // 设备单选表单
  .equipments .el-form-item__content {
    display: block;
    border: 0px;
  }
}
</style>

<style lang="scss" scoped>
$page_bg: #f7f9fe;
$dark_gray: #889aa4;
$light_gray: #dfe4ed;
.page-container {
  height: 100%;
  .top-bar {
    height: 60px;
    line-height: 60px;
    background-color: #44b1f4;
  }
  .login-container {
    min-height: calc(100% - 60px);
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: $page_bg;
    .title-container {
      user-select: none;
      color: #333333;
    }
    .login-form {
      min-width: 300px;
      width: 400px;
      padding: 40px 30px;
      border: 1px solid #cccccc;
      border-radius: 10px;
      background-color: #fff;
      box-shadow: rgba(0, 0, 0, 0.15) 3.2px 3.2px 3.2px;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
}
// 注册
.registered{
  text-align: center;
    .title{
      color:#005980 ;
      cursor: pointer;
      text-decoration: underline;
    }
}
</style>
